package org.yinweichen.llm_dataset_backend.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.yinweichen.llm_dataset_backend.DTO.VersionCreateRequest;
import org.yinweichen.llm_dataset_backend.entity.Version;
import org.yinweichen.llm_dataset_backend.service.VersionService;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/versions")
public class VersionController {

    private final VersionService versionService;

    public VersionController(VersionService versionService) {
        this.versionService = versionService;
    }

    @PostMapping
    public ResponseEntity<?> createVersion(@RequestBody VersionCreateRequest request) {
        try {
            // 参数验证
            if (request.newVer() == null||request.newVer().isEmpty()) {
                return ResponseEntity.badRequest().body("New version number is required");
            }
            if (request.description() == null || request.description().isEmpty()) {
                return ResponseEntity.badRequest().body("Description is required");
            }

            // 调用服务创建版本
            Version newVersion = versionService.createVersion(
                request.baseVer(),
                request.newVer(),
                request.description()
            );

            return ResponseEntity.ok(newVersion);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to create version");
        }
    }

    @PutMapping("/{versionId}/publish")
    public ResponseEntity<?> publishVersion(@PathVariable String versionId) {
        try {
            Version publishedVersion = versionService.publishVersion(versionId);
            return ResponseEntity.ok(publishedVersion);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to publish version");
        }
    }

    /**
     * 获取某一版本详情
     * @param versionId
     * @return
     */
    @GetMapping("/{versionId}")
    public ResponseEntity<?> getVersion(@PathVariable String versionId) {
        try {
            Version version = versionService.getVersion(versionId);
            return ResponseEntity.ok(version);
        } catch (IllegalArgumentException e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to retrieve version");
        }
    }

    /**
     * 获取所有版本详情
     * @return
     */
    @GetMapping
    public ResponseEntity<?> getAllVersions() {
        try {
            return ResponseEntity.ok(versionService.getAllVersions());
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to get versions");
        }
    }
}
